using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
using System;
using UnityEngine.Scripting;

public class DialogService : Service
{
    [Preserve]
    public DialogService()
    {

    }
    public override void OnCreate()
    {
        DialogManager.OnProcessDialogTaskAsync = OnProcessAsync;
    }

    public override async Task OnPreperAsync()
    {
        //await RefManager.Stuff.Main.GetOrRefAsync<GameObject>(nameof(DialogFloating));
    }

    async Task<DialogResult> OnProcessAsync(DialogRequest info)
    {
        if(!Application.isPlaying)
        {
            return default;
        }
        var tcs = new TaskCompletionSource<DialogResult>();
        var result = new DialogResult();
        if(info.style == DialogStyle.ConfirmOnly)
        {
            var dialog = UIEngine.Stuff.ShowFloatingImediatly<DialogFloating>();
            dialog.Title = info.title;
            dialog.Content = info.content;
            dialog.Style = DialogFloating.StyleType.ConfirmOnly;
            //dialog.ButtonGroup = DialogFloatingButtonGroup.ConfirmOnly;
            await ActionUtil.WaitInvokeAsync(ref dialog.onCompleted, callback => dialog.onCompleted -= callback);
            result.choice = true;
            return result;
        }
        else if (info.style == DialogStyle.AdConfirmCancel)
        {
            var dialog = UIEngine.Stuff.ShowFloatingImediatly<DialogFloating>();
            dialog.Title = info.title;
            dialog.Content = info.content;
            dialog.Style = DialogFloating.StyleType.Ask;
            //dialog.ButtonGroup = DialogFloatingButtonGroup.ConfirmCancel;
            var choice = await ActionUtil.WaitInvokeAsync(ref dialog.onCompleted, callback => dialog.onCompleted -= callback);
            result.choice = choice;
            return result;
        }
        else if(info.style == DialogStyle.Reward)
        {
            //var f = await UIEngine.Stuff.ShowFloatingAsync<RewardFloating>();
            //var param = info.param as RewardDialogParam;
            //f.Title = param.title ?? RewardFloating.TitleType.Get;
            //f.AddItemList(param.dataList);
            //await ActionUtil.WaitInvokeAsync(ref f.onConfirm, callback => f.onConfirm -= callback);
            //result.choice = true;
            //return result;
        }
        else if(info.style == DialogStyle.StudyFail)
        {
            //var f = await UIEngine.Stuff.ShowFloatingAsync<StudyFailFloating>();
            //var param = info.param as StudyFailFloatingParam;
            //f.AddItemList(param.dataList);
            //await ActionUtil.WaitInvokeAsync(ref f.onConfirm, callback => f.onConfirm -= callback);
            //result.choice = true;
            //return result;
        }
        else if(info.style == DialogStyle.LevelUpgrade)
        {
            //var param = info.param as UpgradeDialogParam;
            //var f = await UIEngine.Stuff.ShowFloatingAsync<LevelUpgradeFloating>();
            //f.Level = param.toLevel;
            //await ActionUtil.WaitInvokeAsync(ref f.onConfirmed, callback => f.onConfirmed -= callback);
            //result.choice = true;
            //return result;
        }
        else
        {
            Debug.LogError("dialog style: " + info.style + " not support, returns ok");
            result.choice = true;
            tcs.SetResult(result);
        }
        throw new Exception("unsupport dialog style: " + info.style);
    }
}
